#!/bin/sh
# PCP QA Test No. 1533
# Test Linux ZFS PMDA with valgrind
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_check_valgrind
[ $PCP_PLATFORM = linux ] || _notrun "Linux ZFS PMDA is Linux-specific (uses procfs)"
[ -d $PCP_PMDAS_DIR/zfs ] || _notrun "Linux ZFS PMDA not installed"

status=1
trap "cd $here; $sudo rm -rf $tmp.*; exit \$status" 0 1 2 3 15

if [ ! -f $PCP_PMDAS_DIR/zfs/help.pag ]
then
    # PMDA has not been Install'd ... we need the compiled help files
    # for this test
    #
    cd $PCP_PMDAS_DIR/zfs
    $sudo $PCP_BINADM_DIR/newhelp -n root -o help <help
    cd $here
fi

_filter()
{
    $PCP_AWK_PROG '
BEGIN			{ outfile = "'$tmp.0'" }
/^=== std err ===$/	{ outfile = "'$tmp.1'" }
			{ print >outfile }'
    if [ ! -f $tmp.0 ]
    then
	echo "Botch: no part 0 output?"
	touch $tmp.0
    fi
    if [ ! -f $tmp.1 ]
    then
	echo "Botch: no part 1 output?"
	touch $tmp.1
    fi
    ( $here/src/sortinst <$tmp.0; cat $tmp.1 ) \
    | sed \
        -e '/inst /s/\[[01] /[0-or-1 /' \
        -e "s,$PCP_PMDAS_DIR,PCP_PMDAS_DIR,g" \
        -e "s,$PCP_VAR_DIR,PCP_VAR_DIR,g" \
    #end
}

root=$tmp.root
pmns=$PCP_PMDAS_DIR/zfs/root
pmda=$PCP_PMDAS_DIR/zfs/pmda_zfs.so,zfs_init
tgz=$here/linux/zfs-stats.v2.tgz
$sudo rm -f $PCP_VAR_DIR/config/pmda/153.*
$sudo rm -fr $root
mkdir $root || _fail "root in use when processing $tgz"
cd $root
$sudo tar xzf $tgz
export ZFS_PATH=$root/zfs
metrics=`pminfo -n $pmns zfs | LC_COLLATE=POSIX sort`
echo "== Valgrind checking metric descriptors and values"
_run_valgrind pminfo -L -K clear -K add,153,$pmda -n $pmns -dfmtT $metrics | _filter
echo && echo "== done" && echo
cd $here

# success, all done
status=0
exit
